package com.one.efaimaly.order.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialItem;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.BaseExtra;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IListView;
import com.one.efaimaly.order.model.OrderModel;
import com.one.efaimaly.order.model.extra.AddMaterialListExtra;

import java.util.ArrayList;
import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午8:26
 * description ： 添加材料
 **/
public class MaterialPresenter extends BaseApiPresenter<IListView, OrderModel> {
    private List<MaterialItem> allData;
    private DefaultExtra extra;
    private AddMaterialListExtra listExtra;

    private List<AddMaterialItem> freeItems = new ArrayList<>();
    private List<AddMaterialItem> payItems = new ArrayList<>();

    public MaterialPresenter(IListView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Object intentData = mActivity.getIntent().getSerializableExtra(BaseExtra.getExtraName());
        if (intentData != null) {
            if (intentData instanceof DefaultExtra) {
                // 订单id
                extra = (DefaultExtra) intentData;
            } else if (intentData instanceof AddMaterialListExtra) {
                // 材料列表
                listExtra = (AddMaterialListExtra) intentData;
                freeItems = listExtra.getFreeList();
                payItems = listExtra.getPayList();
            }
        }
    }

    public AddMaterialListExtra getListExtra() {
        return listExtra;
    }

    /**
     * 获取材料
     */
    public void getMaterial(final String filter) {
        if (mView != null) {
            if (StringUtils.isBlank(filter) && ListUtils.isNotEmpty(allData)) {
                mView.replaceData(allData);
                return;
            }
            mModel.getMaterial(mView.getPage() + "", filter, new ObserverOnResultListener<List<MaterialItem>>() {
                @Override
                public void onResult(List<MaterialItem> materialItems) {
                    if (StringUtils.isBlank(filter)) {
                        allData = materialItems;
                        mView.loadSuccess(materialItems);
                    } else {
                        mView.replaceData(materialItems);
                    }
                }
            });
        }
    }

    public void getMaterial() {
        getMaterial(null);
    }

    /**
     * 跳转添加页面修改
     */
    public void updateToMaterials(boolean isUpdate) {
        if (listExtra != null) {
            listExtra.setUpdate(isUpdate);
            RouterManager.getInstance()
                    .go(RouterPath.ADD_MATERIAL, listExtra);
        }
        if (mView != null) mView.finishPage();
    }


    /**
     * 获取需要添加的材料
     */
    public List<AddMaterialItem> getAddMaterials() {
        List<AddMaterialItem> addMaterialItems = new ArrayList<>();
        if (mView != null && mView.getData().size() > 0) {
            for (Object item : mView.getData()) {
                if (item instanceof AddMaterialItem) {
                    AddMaterialItem materialItem = (AddMaterialItem) item;
                    if (StringUtils.isNotBlank(materialItem.getName())) {
                        addMaterialItems.add(materialItem);
                        Logger.d("总金额 "+materialItem.getPrice());
                    }
                }
            }
        }
        return addMaterialItems;
    }


    /**
     * 添加材料
     */
    public void addMaterials() {
        if (listExtra != null) {
            mModel.addMaterial(listExtra.getOrderId(), new Gson().toJson(getAddMaterials()), new ObserverOnResultListener<String>() {
                @Override
                public void onResult(String s) {
                    Logger.d("添加材料 success " + s);
                    if (mView != null) {
                        mView.finishPage();
                        // 去订单详情界面
                    }
                }
            });
        }
    }

    /**
     * 得到总价
     */
    public int getTotalPrice() {
        int total = 0;
        for (AddMaterialItem item : getAddMaterials()) {
            Logger.d("总金额 " + item.getNum() + " " + item.getPrice());
            total += (item.getNum() * item.getPrice());
        }
        Logger.d("总金额 " + total);
        return total;
    }
    public List<AddMaterialItem> getFreeItems() {
        return freeItems;
    }

    public List<AddMaterialItem> getPayItems() {
        return payItems;
    }
}
